package com.dictionary.cn.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.dictionary.cn.core.DictionaryManager;
import com.dictionary.cn.core.dictionaryservice.dcitionarybean.Dictionary;
import com.dictionary.cn.core.dictionaryservice.dcitionarybean.TreeDictionary;

/**
 * 
 * <p>
 * Description:	js的缓存处理类 不启用默认的过滤  过滤在前端做
 * </p>
 * @author xiaoruihu
 * @date 2015-6-4 下午12:37:59
 */
@Controller("dictionaryAction")
public class DictionaryAction {

    @Autowired
    private DictionaryManager dictionaryManager;
    /**
     * 
     * <p>
     * 通过key获取dic
     * </p>
     * @author xiaoruihu
     * @date 2015-8-3 下午02:24:07
     * @param request
     * @return
     */
    public Dictionary getDicByKey(HttpServletRequest request){
        
        String type = request.getParameter("type");
        String key = request.getParameter("key");
        
        if(StringUtils.isBlank(type) || StringUtils.isBlank(key)){
            throw new DictionaryException("没有type或者key参数");
        }
        
        return this.dictionaryManager.getDicByKey(type, key);
    }
    
    /**
     * 
     * <p>
     * 获取字典列表
     * </p>
     * @author xiaoruihu
     * @date 2015-8-3 下午02:24:24
     * @param request
     * @return
     */
    public Map<String, List<Dictionary>> getDicAll(HttpServletRequest request){
        
        String[] types = request.getParameterValues("types");
        
        if(types == null){
            throw new DictionaryException("没有types参数");
        }
        Map<String, List<Dictionary>> result = new HashMap<String, List<Dictionary>>(types.length);
        for(String type : types){
            result.put(type, this.dictionaryManager.getAll(type, false));
        }
        return result;
    }
    
    /**
     * 
     * <p>
     * 获取树形的子列表
     * </p>
     * @author xiaoruihu
     * @date 2015-8-3 下午02:24:43
     * @param request
     * @return
     */
    public List<TreeDictionary> getDicChildren(HttpServletRequest request){
        String type = request.getParameter("type");
        String parent = request.getParameter("parent");
        if("NULL".equals(parent)){
            parent = null;
        }
        if(StringUtils.isBlank(type)){
            throw new DictionaryException("没有type参数");
        }
        return this.dictionaryManager.getChildren(type, parent, true);
    }
}









